var editIndex = undefined;
var editIndexSort = undefined;

var sortType = {
    '升序': 'ASC',
    '降序': 'DESC'
};

function onClickCell(index, field, value) {
    b
}

function getSaveArgs() {
    var args = {};
    // 字段信息
    var fieldInfos = $('#columnsDataGrid').datagrid('getChecked');
    if (fieldInfos.length == 0) {
        return args;
    }
    var fieldsArry = [];
    var tableNameArry = [];
    $.each(fieldInfos, function(index, row) {
        var o = {
            TABLE_NAME: row.TABLE_NAME,
            COL_NAME: row.COL_NAME,
            COL_LABEL: row.COL_LABEL
        };
        fieldsArry.push(o);

        var tableName = o.TABLE_NAME;
        var flag = false;
        for (var i = 0; i < tableNameArry.length; i++) {
            if (tableNameArry[i] == tableName) {
                flag = true;
                break;
            }
        }
        if (!flag) {
            tableNameArry.push(tableName);
        }

    });

    // 关联关系
    var relationRows = $('#realationDatagrid').datagrid('getRows');
    var relationArry = [];
    $.each(relationRows, function(index, row) {
        var o = {
            COLNAME: tableInfo[row.COLNAME],
            OPRATION: row.OPRATION,
            COLNAME1: tableInfo[row.COLNAME1]
        };
        relationArry.push(o);
    });
    // 包含的表名称
    // 排序关系
    var sortRows = $('#sortDatagrid').datagrid('getRows');
    var sortRowsArry = [];
    $.each(sortRows, function(index, row) {
        var o = {
            COLNAME3: tableInfo[row.COLNAME3],
            SORT: sortType[row.SORT]
        };
        sortRowsArry.push(o);
    });
    args = {
        sysId: sysId,
        _parentId: parentId,
        reportId: templateId,
        fieldInfos: fieldsArry,
        relation: relationArry,
        sort: sortRowsArry,
        tableNames: tableNameArry
    };
    return args;

}
/**
 *remind 提示
 *chooseconditiondeleted 请选择要删除的条件！
 */
function removeit() {
    var checkRows = $('#realationDatagrid').datagrid('getChecked');
    if (checkRows.length == 0) {
        $.messager.alert(commonLang.tip.alert, commonLang.tip.chooseConditionDeleted, 'warning');
        return;
    }

    $.each(checkRows, function(index, row) {
        var index = $('#realationDatagrid').datagrid('getRowIndex', row);
        $('#realationDatagrid').datagrid('deleteRow', index);

    });

}

/**
 *remind 提示
 *chooseconditiondeleted 请选择要删除的条件！
 */
function removeitSort() {
    var checkRows = $('#sortDatagrid').datagrid('getChecked');
    if (checkRows.length == 0) {
        $.messager.alert(commonLang.tip.alert, commonLang.tip.chooseConditionDeleted, 'warning');
        return;
    }
    $.each(checkRows, function(index, row) {
        var index = $('#sortDatagrid').datagrid('getRowIndex', row);
        $('#sortDatagrid').datagrid('deleteRow', index);
    });

}



// 添加一个条件
/**
 *remind 提示
 *atleasttwofield 请至少选择两个字段！
 */
function append() {
    var checkRows = $('#columnsDataGrid').datagrid('getChecked');
    if (checkRows.length < 2) {
        $.messager.alert(commonLang.tip.alert, commonLang.tip.atLeastTwoField, 'warning');
        return;
    }
    if (endEditing()) {
        $('#realationDatagrid').datagrid('appendRow', {
            COLNAME: '',
            OPRATION: '=',
            COLNAME1: ''
        });
        editIndex = $('#realationDatagrid').datagrid('getRows').length - 1;
        // options:{required:true,panelHeight: 'auto',valueField:'op',textField:'opName',data:[{op:'=',opName:'='}],editable:false}}
        var checkRows = $('#columnsDataGrid').datagrid('getChecked');
        var datas = [];
        $.each(checkRows, function(index, row) {
            var o = {
                colName: row.TABLE_TITLE + "." + row.COL_LABEL,
                colValue: row.TABLE_TITLE + "." + row.COL_LABEL
            };
            datas.push(o);
        });
        $("#realationDatagrid").datagrid('removeEditor', 'COLNAME');
        $("#realationDatagrid").datagrid('addEditor', {
            field: 'COLNAME',
            editor: {
                type: 'combobox',
                options: {
                    required: true,
                    valueField: 'colValue',
                    textField: 'colName',
                    data: datas
                }
            }
        });

        var checkRows1 = $('#columnsDataGrid').datagrid('getChecked');
        var datas1 = [];

        $.each(checkRows1, function(index, row) {
            var o = {
                colName: row.TABLE_TITLE + "." + row.COL_LABEL,
                colValue: row.TABLE_TITLE + "." + row.COL_LABEL
            };
            datas1.push(o);

        });

        $("#realationDatagrid").datagrid('removeEditor', 'COLNAME1');
        $("#realationDatagrid").datagrid('addEditor', {
            field: 'COLNAME1',
            editor: {
                type: 'combobox',
                options: {
                    required: true,
                    valueField: 'colValue',
                    textField: 'colName',
                    data: datas1,
                    tipPosition : 'left'
                }
            }
        });
        $('#realationDatagrid').datagrid('beginEdit', editIndex);
    }
}

/**
 *remind 提示
 *atleasttwofield 请至少选择两个字段！
 */
function appendSort() {
    var checkRows = $('#columnsDataGrid').datagrid('getChecked');
    if (checkRows.length < 2) {
        $.messager.alert(commonLang.tip.alert, commonLang.tip.atLeastTwoField, 'warning');
        return;
    }
    if (endEditingSort()) {
        $('#sortDatagrid').datagrid('appendRow', {
            COLNAME3: '',
            SORT: ''
        });
        editIndexSort = $('#sortDatagrid').datagrid('getRows').length - 1;
        // options:{required:true,panelHeight: 'auto',valueField:'op',textField:'opName',data:[{op:'=',opName:'='}],editable:false}}

        var checkRows = $('#columnsDataGrid').datagrid('getChecked');
        var datas = [];
        $.each(checkRows, function(index, row) {
            var o = {
                colName: row.TABLE_TITLE + "." + row.COL_LABEL,
                colValue: row.TABLE_TITLE + "." + row.COL_LABEL
            };
            datas.push(o);

        });
        $("#sortDatagrid").datagrid('removeEditor', 'COLNAME3');
        $("#sortDatagrid").datagrid('addEditor', {
            field: 'COLNAME3',
            editor: {
                type: 'combobox',
                options: {
                    required: true,
                    valueField: 'colValue',
                    textField: 'colName',
                    data: datas
                }
            }
        });
        $('#sortDatagrid').datagrid('beginEdit', editIndexSort);
    }
}

//onChange: typeSelectSort,


function typeSelect() {

//    if ($('#realationDatagrid').datagrid('validateRow', editIndex)) {
//        $('#realationDatagrid').datagrid('endEdit', editIndex);
//        editIndex = undefined;
//    }

}

function typeSelectSort() {
//	alert('changer');
//    var editor = $('#sortDatagrid').datagrid('getEditor',{index:editIndexSort,field:'COLNAME3'});
//    var editor1 = $('#sortDatagrid').datagrid('getEditor',{index:editIndexSort,field:'SORT'});
//    var colNameValue = ($(editor.target).combobox('getValue'));
//    var sortValue = ($(editor1.target).combobox('getValue'));
//    if('' == colNameValue || '' == sortValue){
//    	  return ;
//    }else {
//    	 $('#sortDatagrid').datagrid('endEdit', editIndexSort);
//         editIndexSort = undefined;
//    }
    // 当最后一个combobox选中的时候 ， 检验所以得内容是否正确，如果正确则关闭
//    if ($('#sortDatagrid').datagrid('validateRow', editIndexSort)) {
//        $('#sortDatagrid').datagrid('endEdit', editIndexSort);
//        editIndexSort = undefined;
//
//    }

}


function selectRows(rowIndex, rowData) {




}


/**
 * 编辑数据
 *
 *remind 提示
 *checksortisright 请检查排序关系数据是否正确！
 */
function endEditing() {
    if (editIndex == undefined) {
        return true;
    }
    if ($('#realationDatagrid').datagrid('validateRow', editIndex)) {
        $('#realationDatagrid').datagrid('endEdit', editIndex);
        editIndex = undefined;
        return true;
    } else {
        $.messager.show({
            title: commonLang.tip.alert,
            msg: commonLang.tip.checkSortIsRight,
            timeout: 3000,
            showType: 'slide'
        });
        return false;
    }
}



/**
 *
 *remind 提示
 *checksortisright 请检查排序关系数据是否正确！
 */

function endEditingSort(remind,checksortisright) {
    if (editIndexSort == undefined) {
        return true;
    }
    if ($('#sortDatagrid').datagrid('validateRow', editIndexSort)) {
        $('#sortDatagrid').datagrid('endEdit', editIndexSort);
        editIndex = undefined;
        return true;
    } else {
        $.messager.show({
            title: commonLang.tip.alert,
            msg: commonLang.tip.checkSortIsRight,
            timeout: 3000,
            showType: 'slide'
        });
        return false;
    }

}




$.extend($.fn.datagrid.methods, {
    editCell: function(jq, param) {
        return jq.each(function() {
            var fields = $(this).datagrid('getColumnFields', true).concat(
                $(this).datagrid('getColumnFields'));
            for (var i = 0; i < fields.length; i++) {
                var col = $(this).datagrid('getColumnOption', fields[i]);
                col.editor1 = col.editor;
                if (fields[i] != param.field) {
                    var fieldOpts = $(this).datagrid('getColumnOption',
                        fields[i]);
                    if (!fieldOpts.hidden || fieldOpts.hidden == false) {
                        col.editor = null;
                    }
                }
            }
            $(this).datagrid('beginEdit', param.index);
            for (var i = 0; i < fields.length; i++) {
                var col = $(this).datagrid('getColumnOption', fields[i]);
                col.editor = col.editor1;
            }
        });
    },
    addEditor: function(jq, param) {
        if (param instanceof Array) {
            $.each(param, function(index, item) {
                var e = $(jq).datagrid('getColumnOption', item.field);
                e.editor = item.editor;
            });
        } else {
            var e = $(jq).datagrid('getColumnOption', param.field);
            e.editor = param.editor;
        }
    },
    removeEditor: function(jq, param) {
        if (param instanceof Array) {
            $.each(param, function(index, item) {
                var e = $(jq).datagrid('getColumnOption', item);
                e.editor = {};
            });
        } else {
            var e = $(jq).datagrid('getColumnOption', param);
            e.editor = {};
        }
    }
});